package modellayer;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import controllayer.UpdateIncompleteException;

/**
 * The Class Order.
 *@author Benjamin Ørskov
 *@author Lasse Lambertsen
 *@author Ole Kirk
 *@author Søren Søgaard
 */
public class Order {

	/** The part orders. */
	private ArrayList<PartOrder> partOrders;

	/** The salesman. */
	private Salesman salesman;

	/** The customer. */
	private Customer customer;

	/** The order id. */
	private int orderID;

	/** The date. */
	private String date;

	/** The price. */
	private Double price;

	/**
	 * Instantiates a new order.
	 *
	 * @param orderID
	 *            the order id
	 */
	public Order(int orderID) {
		DateFormat df = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
		Date dateobj = new Date();
		this.date = df.format(dateobj);
		partOrders = new ArrayList<PartOrder>();
		price = 0.0;
		this.orderID = orderID;
	}

	/**
	 * Adds the part order.
	 *
	 * @param po
	 *            the po
	 */
	public void addPartOrder(PartOrder po) {
		partOrders.add(po);
		price += po.getPartPrice();
	}

	/**
	 * Removes the part order.
	 *
	 * @param po
	 *            the po
	 */
	public void removePartOrder(PartOrder po) {
		price -= po.getPartPrice();
		partOrders.remove(po);
		if (partOrders.contains(po) == true) {
			throw new UpdateIncompleteException("Vare ikke fjernet!");
		}
	}

	/**
	 * Gets the part orders.
	 *
	 * @return the part orders
	 */
	public ArrayList<PartOrder> getPartOrders() {
		return partOrders;
	}

	/**
	 * Removes the item.
	 *
	 * @param itemID
	 *            the item id
	 */
	public void removeItem(int itemID) {
		boolean found = false;
		for (int i = 0; i < partOrders.size() && !found; i++) {
			if (partOrders.get(i).getItem().getItemID() == itemID) {
				partOrders.remove(i);
				found = true;
			}
		}
	}

	/**
	 * Gets the salesman.
	 *
	 * @return the salesman
	 */
	public Salesman getSalesman() {
		return salesman;
	}

	/**
	 * Sets the salesman.
	 *
	 * @param salesman
	 *            the new salesman
	 */
	public void setSalesman(Salesman salesman) {
		this.salesman = salesman;
	}

	/**
	 * Gets the customer.
	 *
	 * @return the customer
	 */
	public Customer getCustomer() {
		return customer;
	}

	/**
	 * Sets the customer.
	 *
	 * @param customer
	 *            the new customer
	 */
	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	/**
	 * Gets the order id.
	 *
	 * @return the order id
	 */
	public int getOrderID() {
		return orderID;
	}

	/**
	 * Sets the order id.
	 *
	 * @param orderID
	 *            the new order id
	 */
	public void setOrderID(int orderID) {
		this.orderID = orderID;
	}

	/**
	 * Gets the date.
	 *
	 * @return the date
	 */
	public String getDate() {
		return date;
	}

	/**
	 * Gets the price.
	 *
	 * @return the price
	 */
	public Double getPrice() {
		return price;
	}

	/**
	 * Sets the price.
	 *
	 * @param price
	 *            the new price
	 */
	public void setPrice(Double price) {
		this.price = price;
	}

	/**
	 * Update amounts.
	 */
	public void updateAmounts() {
		for (PartOrder po : partOrders) {
			po.getItem().setAmount(po.getItem().getAmount() - po.getAmount());
		}
		// TODO when po.getItem().getAmount() < than a specified amount, order
		// new items
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		String returnString = "Dato: " + date + "\nOrdrenummer: " + orderID
				+ "\nKunde: " + customer.getName() + "\nSælger: "
				+ salesman.getName() + "\nVarer: " + partOrders + "\nPris: "
				+ price;
		return returnString;
	}
}